//
//  YRJFindNextStationForAllViewController.m
//  YanRenjie
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 yanrenjie. All rights reserved.
//

#import "YRJFindNextStationForAllViewController.h"
#import "YRJTripHeader.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import <MBProgressHUD.h>

@interface YRJFindNextStationForAllViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionViewFlowLayout *myFlowLayout;
@property(nonatomic, retain)UICollectionView *myCollectionView;
@property(nonatomic, retain)MBProgressHUD *hud;
@property(nonatomic, retain)NSMutableArray *findNextStationForAllArr;
@end

@implementation YRJFindNextStationForAllViewController
- (void)dealloc {
    [_myCollectionView release];
    [_myFlowLayout release];
    [_hud release];
    [_findNextStationForAllArr release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%f", WIDTH);
    NSLog(@"%f", HEIGHT);
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.myFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.myFlowLayout.itemSize = CGSizeMake((160.0 / 375) * WIDTH, (180.0 / 667) * HEIGHT);
    self.myFlowLayout.minimumInteritemSpacing = (10.0 / 375) * WIDTH;
    self.myFlowLayout.minimumLineSpacing = (5.0 / 667) * HEIGHT;
    self.myFlowLayout.sectionInset = UIEdgeInsetsMake((10.0 / 667) * HEIGHT, (15.0 / 375) * WIDTH, (10.0 / 667) * HEIGHT, (15.0 / 375) * WIDTH);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) collectionViewLayout:self.myFlowLayout];
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myCollectionView];
    [self.myCollectionView registerClass:[YRJFindNextStationForAllCollectionViewCell class] forCellWithReuseIdentifier:@"findNextStationCellReuse"];
    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    [_myCollectionView release];
    [_myFlowLayout release];
    
    // 占位图
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"Loading...";
    [self createData];
}

#pragma mark - 普通方法
- (void)createData {
    NSString *strURL = @"http://open.qyer.com/qyer/special/topic/special_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=867739013144832&track_app_version=6.8.1&track_app_channel=hiapk&track_device_info=bbk77_cu_ics2&track_os=Android4.0.4&app_installtime=1445390305001&lat=38.883424&lon=121.544433&page=1&count=10";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.findNextStationForAllArr = [YRJFindNextStationForAllModel baseModelByArr:dic[@"data"]];
        self.hud.hidden = YES;
        [self.myCollectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}


#pragma mark - collectionView 连个必须实现的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.findNextStationForAllArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YRJFindNextStationForAllCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"findNextStationCellReuse" forIndexPath:indexPath];
    [cell.photoImageView sd_setImageWithURL:[NSURL URLWithString:[self.findNextStationForAllArr[indexPath.row] photo]] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
    return cell;
}

#pragma mark - collectionView 的普通协议方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    YRJFindNextStationForAllWebViewController *nextStationWebViewVC = [[YRJFindNextStationForAllWebViewController alloc] init];
    [self.navigationController pushViewController:nextStationWebViewVC animated:YES];
    nextStationWebViewVC.string = (NSString *)[self.findNextStationForAllArr[indexPath.row] url];
    [nextStationWebViewVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
